﻿using System.Collections.Generic;
using System.Windows.Forms;
using ini;

namespace InIReaderExample
{
	/// <summary>
	/// A Class with all example functions.
	/// </summary>
	internal static class Example
	{
		/// <summary>
		/// Creates a empty reader.
		/// </summary>
		/// <returns></returns>
		public static InIReader CreateEmptyReader()
		{
			return new InIReader();
		}

		/// <summary>
		/// Converts the ini reader to a string.
		/// </summary>
		/// <param name="ini">The ini.</param>
		/// <returns></returns>
		public static string ConvertIniReaderToString(InIReader ini)
		{
			return ini.ToString();
		}

		/// <summary>
		/// Foreaches the through INI.
		/// </summary>
		/// <param name="node">The node.</param>
		/// <param name="reader">The reader.</param>
		public static void ForeachThroughIni(TreeNode node, IEnumerable<KeyValuePair<string, InISection>> reader)
		{
			// Alternative use the reader.Keys or reader.Values for iterating just over the keys/values.

			// Foreach throug sections => KeyValuePair<string, InISection>
			foreach (var sections in reader)
			{
				TreeNode child = node.Nodes.Add(sections.Key);
				child.Tag = sections.Value;
				// Foreach throug sections => KeyValuePair<string, string>
				foreach (var entries in sections.Value)
				{
					child.Nodes.Add(entries.Key + " : " + entries.Value).Tag = entries.Value;
				}

				child.Expand();
			}
		}

		/// <summary>
		/// Loads the ini from file.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <returns></returns>
		public static InIReader LoadIniFromFile(string fileName)
		{
			return new InIReader(fileName);
		}

		/// <summary>
		/// Creates the reader from string.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static InIReader CreateReaderFromString(string text)
		{
			InIReader reader = new InIReader();
		reader.Parse(text);
			return reader;
		}

		/// <summary>
		/// Saves the ini to file.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="reader">The reader.</param>
		public static void SaveIniToFile(string fileName, InIReader reader)
		{
			reader.Save(fileName);
		}

		/// <summary>
		/// Merges the ini reader.
		/// </summary>
		/// <param name="iniReader1">The ini reader1.</param>
		/// <param name="iniReader2">The ini reader2.</param>
		/// <param name="replace">if set to <c>true</c> [replace].</param>
		public static void MergeIniReader(InIReader iniReader1, InIReader iniReader2, bool replace)
		{
			iniReader1.Add(iniReader2, replace);
		}
	}
}